package com.ght.springboot.rabbitmq.customer;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.PostConstruct;

/**
 * @description:
 * @author: haitao.gao
 * @create: 2019-01-09 17:29
 **/
public class BaseQueueCustomer extends SimpleMessageListenerContainer implements ChannelAwareMessageListener,InitializingBean {

    @Autowired
    @Qualifier("connectionFactoryForBusiness")
    private ConnectionFactory connectionFactory;

    @PostConstruct
    private void init(){setConnectionFactory(connectionFactory);}

    private static Integer times = 0;


    public BaseQueueCustomer(Queue queue){
        setQueues(queue);
        setExposeListenerChannel(true);
        setMaxConcurrentConsumers(1);
        setConcurrentConsumers(1);
        setMissingQueuesFatal(false);
        setAcknowledgeMode(AcknowledgeMode.MANUAL); //设置确认模式手工确认
        setMessageListener(this);
    }

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {

//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
    }
}
